package com.epam.weblibrary.service.user;

import com.epam.weblibrary.entities.user.User;
import com.epam.weblibrary.repositories.user.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

/**
 * Created by Iurii_Miedviediev on 9/12/2014.
 */
public class CustomUserDetailsService implements UserDetailsService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public UserDetails loadUserByUsername(String login)
            throws UsernameNotFoundException {

        User domainUser = userRepository.findUserByLogin(login);

        if(domainUser == null) {
            throw new UsernameNotFoundException("User with login " + login + " not found.");
        }

        return domainUser;
    }
}
